using APP.HELPER.Data.Helper;
using APP.HELPER.Utils;
using APP.HELPER.Utils.CustomizeToolbox;
using APP.HELPER.Utils.Security;
using APP.HELPER.Utils.System;
using DevExpress.XtraEditors;
using Microsoft.VisualBasic;
using System;
using System.ComponentModel;
using System.IO;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Windows.Forms;

namespace Service
{
    public class CODE
    {
        public delegate void LicenseCheckedEventHanlder(object sender);
        public const string Consys = "tOPIxxYos7q7KFheUFh/Zczhu/5hC5PryXFA1850bvhMqXE3+8cmhNLR18eIZqCc+X31s2MsNck=";
        private string _mSoftId;
        private string _mSoftName;
        private string _mVersion;
        private int _mType;
        private static string _mContact = Environment.UserName;
        private static string _mCompanyName = Environment.MachineName;
        private static string _mAddress = "Số 3 Đường số 3 Khu dân cư Kiến Phát, Phường 6, TP. Tân An.";
        private string _mTel;
        private string _mMobile;
        private string _mFax;
        private string _mEmail;
        private static string _mWebsite = "http://www.perfect.com.vn";
        private string _mLanguage;
        private DateTime _mCreated;
        private DateTime _mLimit;
        private int _mDay;
        private int _mLicense;
        private string _mCode;
        private int _mNumber;
        private int _mCommand;
        private DateTime _mToday;
        private string _mActive;
        private static string _mComputerCode;
        private static int _mDayEnd;
        private static int _mTypeActive;
        private static int _mTypeSoft;
        private static string _mTrialFile = "Perfect." + GridCalendarControlLib.SignSoft() + ".Shared.dll";
        private static string _mMessage = "Hãy đăng ký phiên bản thu phí để sử dụng đầy đủ tính năng và được sự hỗ trợ tốt nhất của chúng tôi.";
        private readonly byte[] _number = new byte[2];
        private static Random rnk = new Random(26);
        private RandomNumberGenerator rng = RandomNumberGenerator.Create();
        public event CODE.LicenseCheckedEventHanlder LicenseChecked;
        public static string Message
        {
            get
            {
                return CODE._mMessage;
            }
        }
        public static int TypeSoft
        {
            get
            {
                return CODE._mTypeSoft;
            }
            set
            {
                CODE._mTypeSoft = value;
            }
        }
        public static int TypeActive
        {
            get
            {
                return CODE._mTypeActive;
            }
        }
        public static string SoftInfo
        {
            get
            {
                string result;
                if (CODE._mTypeSoft == 0)
                {
                    result = "Trial Edition";
                }
                else
                {
                    if (CODE._mTypeSoft == 1)
                    {
                        result = "Free Edition";
                    }
                    else
                    {
                        if (CODE._mTypeSoft >= 200 & CODE._mTypeSoft <= 299)
                        {
                            result = "Standard Edition";
                        }
                        else
                        {
                            if (CODE._mTypeSoft >= 300 & CODE._mTypeSoft <= 399)
                            {
                                result = "Professional Edition";
                            }
                            else
                            {
                                if (CODE._mTypeSoft >= 400 & CODE._mTypeSoft <= 499)
                                {
                                    result = "Enterprise Edition";
                                }
                                else
                                {
                                    if (CODE._mTypeSoft >= 500 & CODE._mTypeSoft <= 599)
                                    {
                                        result = "Express Edition";
                                    }
                                    else
                                    {
                                        if (CODE._mTypeSoft >= 600 & CODE._mTypeSoft <= 699)
                                        {
                                            result = "Extanded(Packaged) Edition";
                                        }
                                        else
                                        {
                                            result = "Unknown Edition";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                return result;
            }
        }
        public static int DayEnd
        {
            get
            {
                return CODE._mDayEnd;
            }
        }
        [Category("Primary Key"), DisplayName("Soft_Id")]
        public string SoftId
        {
            get
            {
                return this._mSoftId;
            }
            set
            {
                this._mSoftId = value;
            }
        }
        [Category("Column"), DisplayName("Soft_Name")]
        public string SoftName
        {
            get
            {
                return this._mSoftName;
            }
            set
            {
                this._mSoftName = value;
            }
        }
        [Category("Column"), DisplayName("Version")]
        public string Version
        {
            get
            {
                return this._mVersion;
            }
            set
            {
                this._mVersion = value;
            }
        }
        [Category("Column"), DisplayName("Type")]
        public int Type
        {
            get
            {
                return this._mType;
            }
            set
            {
                this._mType = value;
            }
        }
        [Category("Column"), DisplayName("Contact")]
        public static string Contact
        {
            get
            {
                return CODE._mContact;
            }
            set
            {
                CODE._mContact = value;
            }
        }
        [Category("Column"), DisplayName("Company")]
        public static string CompanyName
        {
            get
            {
                return CODE._mCompanyName;
            }
            set
            {
                CODE._mCompanyName = value;
            }
        }
        [Category("Column"), DisplayName("Address")]
        public static string Address
        {
            get
            {
                return CODE._mAddress;
            }
            set
            {
                CODE._mAddress = value;
            }
        }
        [Category("Column"), DisplayName("Tel")]
        public string Tel
        {
            get
            {
                return this._mTel;
            }
            set
            {
                this._mTel = value;
            }
        }
        [Category("Column"), DisplayName("Mobile")]
        public string Mobile
        {
            get
            {
                return this._mMobile;
            }
            set
            {
                this._mMobile = value;
            }
        }
        [Category("Column"), DisplayName("Fax")]
        public string Fax
        {
            get
            {
                return this._mFax;
            }
            set
            {
                this._mFax = value;
            }
        }
        [Category("Column"), DisplayName("Email")]
        public string Email
        {
            get
            {
                return this._mEmail;
            }
            set
            {
                this._mEmail = value;
            }
        }
        [Category("Column"), DisplayName("Website")]
        public static string Website
        {
            get
            {
                return CODE._mWebsite;
            }
            set
            {
                CODE._mWebsite = value;
            }
        }
        [Category("Column"), DisplayName("Language")]
        public string Language
        {
            get
            {
                return this._mLanguage;
            }
            set
            {
                this._mLanguage = value;
            }
        }
        [Category("Column"), DisplayName("Created")]
        public DateTime Created
        {
            get
            {
                return this._mCreated;
            }
            set
            {
                this._mCreated = value;
            }
        }
        [Category("Column"), DisplayName("Limit")]
        public DateTime Limit
        {
            get
            {
                return this._mLimit;
            }
            set
            {
                this._mLimit = value;
            }
        }
        [Category("Column"), DisplayName("Day")]
        public int Day
        {
            get
            {
                return this._mDay;
            }
            set
            {
                this._mDay = value;
            }
        }
        [Category("Column"), DisplayName("License")]
        public int License
        {
            get
            {
                return this._mLicense;
            }
            set
            {
                this._mLicense = value;
            }
        }
        [Category("Column"), DisplayName("Code")]
        public string Code
        {
            get
            {
                return this._mCode;
            }
            set
            {
                this._mCode = value;
            }
        }
        [Category("Column"), DisplayName("Number")]
        public int Number
        {
            get
            {
                return this._mNumber;
            }
            set
            {
                this._mNumber = value;
            }
        }
        [Category("Column"), DisplayName("Command")]
        public int Command
        {
            get
            {
                return this._mCommand;
            }
            set
            {
                this._mCommand = value;
            }
        }
        [Category("Column"), DisplayName("Today")]
        public DateTime Today
        {
            get
            {
                return this._mToday;
            }
            set
            {
                this._mToday = value;
            }
        }
        [Category("Column"), DisplayName("Active")]
        public string Active
        {
            get
            {
                return this._mActive;
            }
            set
            {
                this._mActive = value;
            }
        }
        [Category("Column"), DisplayName("ComputerCode")]
        public static string ComputerCode
        {
            get
            {
                return CODE._mComputerCode;
            }
            set
            {
                CODE._mComputerCode = value;
            }
        }
        public string AssemblyVersion
        {
            get
            {
                return "2.5.0.0";
            }
        }
        public string Copyright
        {
            get
            {
                return "Công Ty Phần Mềm Hoàn Hảo";
            }
        }
        private void InvokeLicenseChecked()
        {
            CODE.LicenseCheckedEventHanlder licenseChecked = this.LicenseChecked;
            if (licenseChecked != null)
            {
                licenseChecked(this);
            }
        }
        public void SetTrialFile(string trialfile)
        {
            CODE._mTrialFile = trialfile;
        }
        public static void Notify()
        {
            XtraMessageBox.Show("Chức năng này không được hỗ trợ trong phiên bản miễn phí (Free Edition)\nĐể sử dụng chức năng này vui lòng đăng ký phiên bản thu phí.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        public static void Warning()
        {
            XtraMessageBox.Show("Dữ liệu đã vực qua mức giới hạn 1500 mẩu tin trong phiên bản này.\n Để tiếp tục sử dụng bạn vui lòng kết chuyển dữ liệu sang kỳ mới. Hoặc đăng ký bản quyền để sử dụng gói dữ liệu dành cho doanh nghiệp lớn.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        public CODE()
        {
            this._mSoftId = SystemInfoHelper.GetSoftware(APP.HELPER.Utils.AssemblyHelper.AssemblyTitle);
            this._mSoftName = APP.HELPER.Utils.AssemblyHelper.AssemblyTitle;
            this._mVersion = APP.HELPER.Utils.AssemblyHelper.AssemblyVersion;
            this._mType = 0;
            this._mTel = "";
            this._mMobile = "";
            this._mFax = "";
            this._mEmail = "";
            this._mLanguage = "";
            this._mCreated = DateTime.Now;
            this._mLimit = DateTime.Now;
            this._mDay = 0;
            this._mLicense = 0;
            this._mCode = "";
            this._mNumber = 1;
            this._mCommand = 1;
            this._mToday = DateTime.Now;
            this._mActive = "";
            CODE._mComputerCode = "";
        }
        private static int GetTypeSoft(string code)
        {
            int result;
            if (code.Length == 0)
            {
                //result = 1;
                result = 501;
            }
            else
            {
                string text = code.Substring(16, 3);
                if (text.StartsWith("F"))
                {
                    result = 1;
                }
                else
                {
                    if (text.StartsWith("S"))
                    {
                        result = 200;
                    }
                    else
                    {
                        if (text.StartsWith("P"))
                        {
                            result = 300;
                        }
                        else
                        {
                            if (text.StartsWith("E"))
                            {
                                result = 400;
                            }
                            else
                            {
                                if (text.StartsWith("R"))
                                {
                                    result = 500;
                                }
                                else
                                {
                                    if (text.StartsWith("X"))
                                    {
                                        result = 301;
                                    }
                                    else
                                    {
                                        if (text.StartsWith("B"))
                                        {
                                            result = 302;
                                        }
                                        else
                                        {
                                            if (text.StartsWith("ENB"))
                                            {
                                                result = 501;
                                            }
                                            else
                                            {
                                                result = 0;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return result;
        }
        public bool WriteFile(string filePath)
        {
            filePath = Application.StartupPath + "\\" + filePath;
            bool result;
            try
            {
                FileStream fileStream = File.Create(filePath);
                BinaryWriter binaryWriter = new BinaryWriter(fileStream);
                File.Delete(Application.StartupPath + "\\Perfect.key");
                this._mType = CODE.GetTypeSoft(this._mCode);
                for (int i = 0; i < 30; i++)
                {
                    binaryWriter.Write(MyEncryption.Encrypt(this._mActive, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(CODE._mAddress, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mCode, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mCommand.ToString(), CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(CODE.CompanyName, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(CODE._mComputerCode, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(CODE._mContact, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mCreated.Ticks.ToString(), CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mDay.ToString(), CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mEmail, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mFax, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mLanguage, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mLicense.ToString(), CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mLimit.Ticks.ToString(), CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mMobile, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mNumber.ToString(), CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mSoftId, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mSoftName, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mTel, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mToday.Ticks.ToString(), CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mType.ToString(), CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mVersion, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(CODE._mWebsite, CODE._mComputerCode, true));
                }
                binaryWriter.Close();
                fileStream.Close();
                CryptoProgressCallBack callback = new CryptoProgressCallBack(this.ProgressCallBackEncrypt);
                CryptoHelper.EncryptFile(filePath, Application.StartupPath + "\\Perfect.key", CODE._mComputerCode, callback);
                //File.Delete(filePath);
            }
            catch (Exception var_4_30A)
            {
                result = false;
                return result;
            }
            result = true;
            return result;
        }
        private void ProgressCallBackEncrypt(int min, int max, int value)
        {
        }
        public bool ReadFile(string filePath, string key)
        {
            filePath = Application.StartupPath + "\\" + filePath;
            FileInfo fileInfo = new FileInfo(Application.StartupPath + "\\Perfect.key");
            bool result;
            if (!fileInfo.Exists)
            {
                result = false;
            }
            else
            {
                try
                {
                    CryptoProgressCallBack callback = new CryptoProgressCallBack(this.ProgressCallBackEncrypt);
                    CryptoHelper.DecryptFile(Application.StartupPath + "\\Perfect.key", filePath, key, callback);
                    FileStream fileStream = File.OpenRead(filePath);
                    BinaryReader binaryReader = new BinaryReader(fileStream);
                    this._mActive = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    CODE._mAddress = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mCode = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mCommand = Convert.ToInt32(MyEncryption.Decrypt(binaryReader.ReadString(), key, true));
                    CODE.CompanyName = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    CODE._mComputerCode = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    CODE._mContact = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mCreated = new DateTime(Convert.ToInt64(MyEncryption.Decrypt(binaryReader.ReadString(), key, true)));
                    this._mDay = Convert.ToInt32(MyEncryption.Decrypt(binaryReader.ReadString(), key, true));
                    CODE._mDayEnd = this._mDay;
                    this._mEmail = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mFax = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mLanguage = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mLicense = Convert.ToInt32(MyEncryption.Decrypt(binaryReader.ReadString(), key, true));
                    CODE._mTypeActive = this._mLicense;
                    this._mLimit = new DateTime(Convert.ToInt64(MyEncryption.Decrypt(binaryReader.ReadString(), key, true)));
                    this._mMobile = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mNumber = Convert.ToInt32(MyEncryption.Decrypt(binaryReader.ReadString(), key, true));
                    this._mSoftId = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mSoftName = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mTel = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mToday = new DateTime(Convert.ToInt64(MyEncryption.Decrypt(binaryReader.ReadString(), key, true)));
                    this._mType = Convert.ToInt32(MyEncryption.Decrypt(binaryReader.ReadString(), key, true));
                    CODE._mTypeSoft = this._mType;
                    this._mVersion = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    CODE._mWebsite = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    binaryReader.Close();
                    fileStream.Close();
                    //File.Delete(filePath);
                }
                catch (Exception var_4_291)
                {
                    fileInfo.Delete();
                    result = false;
                    return result;
                }
                result = true;
            }
            return result;
        }
        public bool TrialWriteFile(string filePath)
        {
            filePath = Application.StartupPath + "\\" + filePath;
            bool result;
            try
            {
                FileStream fileStream = File.Create(filePath);
                BinaryWriter binaryWriter = new BinaryWriter(fileStream);
                string text = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles) + "\\Perfect Shared";
                if (!Directory.Exists(text))
                {
                    DirectoryInfo directoryInfo = new DirectoryInfo(text);
                    directoryInfo.Create();
                    DirectorySecurity accessControl = directoryInfo.GetAccessControl();
                    accessControl.SetAccessRule(new FileSystemAccessRule(Environment.UserDomainName + "\\" + Environment.UserName, FileSystemRights.FullControl, AccessControlType.Allow));
                }
                string text2 = text + "\\" + CODE._mTrialFile;
                if (File.Exists(text2))
                {
                    File.Delete(text2);
                }
                for (int i = 0; i < 30; i++)
                {
                    binaryWriter.Write(MyEncryption.Encrypt(this._mActive, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(CODE._mAddress, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mCode, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mCommand.ToString(), CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(CODE.CompanyName, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(CODE._mComputerCode, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(CODE._mContact, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mCreated.Ticks.ToString(), CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mDay.ToString(), CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mEmail, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mFax, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mLanguage, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mLicense.ToString(), CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mLimit.Ticks.ToString(), CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mMobile, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mNumber.ToString(), CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mSoftId, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mSoftName, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mTel, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mToday.Ticks.ToString(), CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mType.ToString(), CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(this._mVersion, CODE._mComputerCode, true));
                    binaryWriter.Write(MyEncryption.Encrypt(CODE._mWebsite, CODE._mComputerCode, true));
                }
                binaryWriter.Close();
                fileStream.Close();
                CryptoProgressCallBack callback = new CryptoProgressCallBack(this.ProgressCallBackEncrypt);
                CryptoHelper.EncryptFile(filePath, text2, CODE._mComputerCode, callback);
                File.Delete(filePath);
            }
            catch (Exception var_8_366)
            {
                result = false;
                return result;
            }
            result = true;
            return result;
        }
        public bool TrialReadFile(string filePath, string key)
        {
            filePath = Application.StartupPath + "\\" + filePath;
            string text = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles) + "\\Perfect Shared\\" + CODE._mTrialFile;
            FileInfo fileInfo = new FileInfo(text);
            bool result;
            if (!fileInfo.Exists)
            {
                result = false;
            }
            else
            {
                try
                {
                    CryptoProgressCallBack callback = new CryptoProgressCallBack(this.ProgressCallBackEncrypt);
                    CryptoHelper.DecryptFile(text, filePath, key, callback);
                    FileStream fileStream = File.OpenRead(filePath);
                    BinaryReader binaryReader = new BinaryReader(fileStream);
                    this._mActive = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    CODE._mAddress = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mCode = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mCommand = Convert.ToInt32(MyEncryption.Decrypt(binaryReader.ReadString(), key, true));
                    CODE.CompanyName = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    CODE._mComputerCode = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    CODE._mContact = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mCreated = new DateTime(Convert.ToInt64(MyEncryption.Decrypt(binaryReader.ReadString(), key, true)));
                    this._mDay = Convert.ToInt32(MyEncryption.Decrypt(binaryReader.ReadString(), key, true));
                    this._mEmail = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mFax = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mLanguage = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mLicense = Convert.ToInt32(MyEncryption.Decrypt(binaryReader.ReadString(), key, true));
                    CODE._mTypeActive = 0;
                    this._mLimit = new DateTime(Convert.ToInt64(MyEncryption.Decrypt(binaryReader.ReadString(), key, true)));
                    CODE._mDayEnd = Convert.ToInt32(DateAndTime.DateDiff(DateInterval.Day, DateTime.Now, this._mLimit, FirstDayOfWeek.Monday, FirstWeekOfYear.System));
                    this._mMobile = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mNumber = Convert.ToInt32(MyEncryption.Decrypt(binaryReader.ReadString(), key, true));
                    this._mSoftId = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mSoftName = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mTel = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    this._mToday = new DateTime(Convert.ToInt64(MyEncryption.Decrypt(binaryReader.ReadString(), key, true)));
                    this._mType = Convert.ToInt32(MyEncryption.Decrypt(binaryReader.ReadString(), key, true));
                    CODE._mTypeSoft = 0;
                    this._mVersion = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    CODE._mWebsite = MyEncryption.Decrypt(binaryReader.ReadString(), key, true);
                    binaryReader.Close();
                    fileStream.Close();
                    File.Delete(filePath);
                    CODE._mMessage = "Phần Mềm còn " + CODE._mDayEnd + " (ngày) hạn sử dụng.\n\nThông Tin Liên Hệ\n\nCông Ty Phần Mềm Hoàn Hảo \n\nĐịa chỉ: 41 Đường Cách Mạng Tháng 8, Phường 1, Thị xã Tân An, Long An \nĐiện thoại: (072) 6251520 \nFax: (072) 6251519\nEmail: sales@perfect.com.vn\nWebsite: www.perfect.com.vn";
                }
                catch (Exception var_5_2CB)
                {
                    result = false;
                    return result;
                }
                result = true;
            }
            return result;
        }
        public bool CheckTrial()
        {
            return this.CheckTrial(30, CODE._mTrialFile);
        }
        public bool CheckTrial(int day)
        {
            return this.CheckTrial(day, CODE._mTrialFile);
        }
        public bool CheckTrial(int day, string trialfile)
        {
            if (!string.IsNullOrEmpty(CODE._mComputerCode))
            {
                SystemInfoHelper.SetDefault();
                CODE._mComputerCode = MyRegister.GenId();
            }
            CODE._mTrialFile = trialfile;
            return this.TrialReadFile("Temp.key", CODE._mComputerCode) || this.MakeTrial(day);
        }
        public bool CheckFree(int day)
        {
            if (string.IsNullOrEmpty(CODE._mComputerCode))
            {
                SystemInfoHelper.SetDefault();
                CODE._mComputerCode = MyRegister.GenId();
            }
            return this.ReadFile("Perfect.Lic", CODE._mComputerCode) || this.MakeFree(day, 1);
        }
        public bool CheckFree()
        {
            return this.CheckFree(-1);
        }
        public bool CheckLicense(int day, string trialfile, bool free)
        {
            SystemInfoHelper.SetDefault();
            CODE._mComputerCode = MyRegister.GenId();
            CODE._mTrialFile = trialfile;
            bool result;
            if (this.ReadFile("Perfect.Lic", CODE._mComputerCode))
            {
                result = true;
            }
            else
            {
                if (this.TrialReadFile("Temp.key", CODE._mComputerCode))
                {
                    if (CODE._mDayEnd <= 0)
                    {
                        this.CheckFree();
                        CODE._mMessage = "Phần Mềm đã hết hạn sử dụng.\n\nThông Tin Liên Hệ\n\nCông Ty Phần Mềm Hoàn Hảo \n\nĐịa chỉ: 41 Đường Cách Mạng Tháng 8, Phường 1, Thị xã Tân An, Long An \nĐiện thoại: (072) 6251520 \nFax: (072) 6251519\nEmail: sales@perfect.com.vn\nWebsite: www.perfect.com.vn";
                    }
                    else
                    {
                        CODE._mMessage = "Mua phiên bản thu phí để được hỗ trợ tốt hơn.\n\nThông Tin Liên Hệ\n\nCông Ty Phần Mềm Hoàn Hảo \n\nĐịa chỉ: 41 Đường Cách Mạng Tháng 8, Phường 1, Thị xã Tân An, Long An \nĐiện thoại: (072) 6251520 \nFax: (072) 6251519\nEmail: sales@perfect.com.vn\nWebsite: www.perfect.com.vn";
                    }
                    result = true;
                }
                else
                {
                    result = this.MakeTrial(day);
                }
            }
            return result;
        }
        public bool CheckLicense(int day, string trialfile)
        {
            SystemInfoHelper.SetDefault();
            CODE._mComputerCode = MyRegister.GenId();
            CODE._mTrialFile = trialfile;
            bool result;
            if (this.ReadFile("Perfect.Lic", CODE._mComputerCode))
            {
                result = true;
            }
            else
            {
                if (this.TrialReadFile("Temp.key", CODE._mComputerCode))
                {
                    if (CODE._mDayEnd <= 0)
                    {
                        this.CheckFree();
                    }
                    result = false;
                }
                else
                {
                    result = this.MakeTrial(day);
                }
            }
            return result;
        }
        public void Execute()
        {
            if (Internet.IsConnectedToInternet())
            {
                SqlHelper sqlHelper = new SqlHelper("tOPIxxYos7q7KFheUFh/Zczhu/5hC5PryXFA1850bvhMqXE3+8cmhNLR18eIZqCc+X31s2MsNck=", "07071983", true);
                if (sqlHelper.Check(true))
                {
                    SystemInfoHelper.SetDefault();
                    CODE._mComputerCode = MyRegister.GenId();
                    try
                    {
                        //if (CODE._mTypeSoft > 1)
                        if (CODE._mTypeSoft == 501)
                        {
                            //if (this.ReadFile("Perfect.Lic", CODE._mComputerCode))
                            {
                                //License license = new License();
                                if (!(sqlHelper.Open() != "OK"))
                                {
                                    sqlHelper.Close();
                                    //if (license.Get(this._mCode, CODE._mComputerCode) == "OK")
                                    {
                                        Keycode keycode = new Keycode();
                                        if (keycode.Get(this._mCode) == "OK")
                                        {
                                            //File.Delete(Application.StartupPath + "\\Perfect.key");
                                            this.MakeFree();
                                        }
                                        if (keycode.Blacklist)
                                        {
                                            //File.Delete(Application.StartupPath + "\\Perfect.key");
                                            this.MakeFree();
                                        }
                                        //if (!license.Active)
                                        {
                                            //File.Delete(Application.StartupPath + "\\Perfect.key");
                                            this.MakeFree();
                                        }
                                    }
                                    //else
                                    {
                                        //File.Delete(Application.StartupPath + "\\Perfect.key");
                                        this.MakeFree();
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception var_3_18C)
                    {
                        XtraMessageBox.Show("Bản quyền phần mềm này không xác định được!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        //File.Delete(Application.StartupPath + "\\Perfect.key");
                    }
                }
            }
        }
        public bool CheckLicense(string trialfile, bool free)
        {
            return this.CheckLicense(30, trialfile, free);
        }
        public bool CheckLicense(bool free)
        {
            return this.CheckLicense(30, CODE._mTrialFile, free);
        }
        public bool CheckLicense()
        {
            return this.CheckLicense(30, CODE._mTrialFile, true);
        }
        public bool ActiveLicense()
        {
            bool result;
            if (!this.CodeMacth())
            {
                result = false;
            }
            else
            {
                this._mLicense = Convert.ToInt32(this._mCode[10].ToString());
                result = this.MakeCode();
            }
            return result;
        }
        public static string GenKey(int version1, int version2, string typesoft, int num, int type)
        {
            string[] array = new string[]
			{
				"0P01",
				"01E1",
				"1R11",
				"FFRE",
				"111E",
				"11C1",
				"111T"
			};
            array[0] = string.Concat(new object[]
			{
				CODE.GetChar(),
				"P",
				version1,
				CODE.GetLetter()
			});
            array[1] = string.Concat(new object[]
			{
				version2,
				CODE.GetChar().ToString(),
				"E",
				CODE.GetLetter()
			});
            array[2] = string.Concat(new object[]
			{
				type,
				"R",
				CODE.GetNumber(),
				CODE.GetLetter()
			});
            array[3] = "F" + CODE.getTypeSoft(typesoft);
            array[4] = CODE.FormatNumber(num.ToString()) + "E";
            array[5] = string.Concat(new object[]
			{
				CODE.GetNumber(),
				CODE.GetNumber().ToString(),
				"C",
				CODE.GetLetter()
			});
            string value = array[5].Substring(0, 2);
            int value2 = Convert.ToInt32(value) % 20 + 65;
            array[1] = array[1].Remove(3);
            array[1] = array[1] + Convert.ToChar(value2);
            array[2] = array[2].Remove(2);
            array[2] = array[2] + Convert.ToInt32(array[5][3]);
            value2 = Convert.ToInt32(array[0][3]) % 15 + 65;
            array[6] = Convert.ToInt32(array[3][3]).ToString() + Convert.ToChar(value2) + "T";
            return string.Join("-", array);
        }
        private static string getTypeSoft(string typesoft)
        {
            string result;
            if (typesoft.StartsWith("PRO"))
            {
                result = "P" + CODE.GetNumber() + CODE.GetNumber();
            }
            else
            {
                if (typesoft.StartsWith("FRE"))
                {
                    result = "F" + CODE.GetNumber() + CODE.GetNumber();
                }
                else
                {
                    if (typesoft.StartsWith("STA"))
                    {
                        result = "S" + CODE.GetNumber() + CODE.GetNumber();
                    }
                    else
                    {
                        if (typesoft.StartsWith("ENT"))
                        {
                            result = "E" + CODE.GetNumber() + CODE.GetNumber();
                        }
                        else
                        {
                            if (typesoft.StartsWith("ENB"))
                            {
                                result = "ENB";
                            }
                            else
                            {
                                if (typesoft.StartsWith("EXP"))
                                {
                                    result = "R" + CODE.GetNumber() + CODE.GetNumber();
                                }
                                else
                                {
                                    if (typesoft.StartsWith("EXT"))
                                    {
                                        result = "X" + CODE.GetNumber() + CODE.GetNumber();
                                    }
                                    else
                                    {
                                        result = "T" + CODE.GetNumber() + CODE.GetNumber();
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return result;
        }
        private static string FormatNumber(string num)
        {
            string result;
            if (num.Length == 1)
            {
                result = "00" + num;
            }
            else
            {
                if (num.Length == 2)
                {
                    result = "0" + num;
                }
                else
                {
                    if (num.Length == 3)
                    {
                        result = num;
                    }
                    else
                    {
                        result = "000";
                    }
                }
            }
            return result;
        }
        public int Rnk()
        {
            this.rng.GetBytes(this._number);
            return (int)BitConverter.ToInt16(this._number, 0);
        }
        internal static char GetChar()
        {
            char result;
            if (CODE.rnk.Next(2) > 1)
            {
                result = CODE.GetChar();
            }
            else
            {
                result = CODE.GetNumber();
            }
            return result;
        }
        internal static char GetNumber()
        {
            return CODE.GetChar(9, 48);
        }
        internal static char GetLetter()
        {
            return CODE.GetChar(26, 65);
        }
        private static char GetChar(int seed, int add)
        {
            return Convert.ToChar(CODE.rnk.Next(seed) + add);
        }
        public bool MakeCode()
        {
            MyEncryption myEncryption = new MyEncryption();
            string text = this._mCode;
            text = MyEncryption.Encrypt(text, CODE._mComputerCode, true);
            text = myEncryption.SHA512Encrypt(text);
            string text2 = text.Substring(7, 4);
            text2 = text2 + "-P" + text.Substring(18, 3);
            text2 = text2 + "-E" + text.Substring(24, 3);
            text2 = text2 + "-R" + text.Substring(30, 3);
            text2 = text2 + "-" + text.Substring(42, 4);
            text2 = text2 + "-F" + text.Substring(51, 3);
            text2 = text2 + "-E" + MyEncryption.InverseString(text.Substring(18, 3));
            text2 = text2 + "-C" + MyEncryption.InverseString(text.Substring(9, 3));
            text2 = text2 + "-T" + MyEncryption.InverseString(text.Substring(36, 3));
            object obj = text2;
            text2 = string.Concat(new object[]
			{
				obj,
				"-",
				text.Substring(51, 3)[2],
				text.Substring(42, 4)[3],
				text.Substring(30, 3)[2],
				text.Substring(24, 4)[3]
			});
            return text2.CompareTo(this._mActive) == 0;
        }
        public string ActiveCode()
        {
            this._mLicense = Convert.ToInt32(this._mCode[10].ToString());
            MyEncryption myEncryption = new MyEncryption();
            string text = this._mCode;
            text = MyEncryption.Encrypt(text, CODE._mComputerCode, true);
            text = myEncryption.SHA512Encrypt(text);
            string text2 = text.Substring(7, 4);
            text2 = text2 + "-P" + text.Substring(18, 3);
            text2 = text2 + "-E" + text.Substring(24, 3);
            text2 = text2 + "-R" + text.Substring(30, 3);
            text2 = text2 + "-" + text.Substring(42, 4);
            text2 = text2 + "-F" + text.Substring(51, 3);
            text2 = text2 + "-E" + MyEncryption.InverseString(text.Substring(18, 3));
            text2 = text2 + "-C" + MyEncryption.InverseString(text.Substring(9, 3));
            text2 = text2 + "-T" + MyEncryption.InverseString(text.Substring(36, 3));
            object obj = text2;
            return string.Concat(new object[]
			{
				obj,
				"-",
				text.Substring(51, 3)[2],
				text.Substring(42, 4)[3],
				text.Substring(30, 3)[2],
				text.Substring(24, 4)[3]
			});
        }
        public bool CodeMacth()
        {
            return this.CodeMacth(this._mCode);
        }
        public bool CodeMacth(string code)
        {
            bool result;
            if (code.IndexOf("0000-0000-0000-0000-0000-0000") != -1)
            {
                result = true;
            }
            else
            {
                if (code.IndexOf("1111-1111-1111-1111-1111-1111") != -1)
                {
                    result = true;
                }
                else
                {
                    this._mCode = code;
                    if (this._mCode.Length != 34)
                    {
                        result = false;
                    }
                    else
                    {
                        if (this._mCode[4] != '-' | this._mCode[9] != '-' | this._mCode[14] != '-' | this._mCode[19] != '-' | this._mCode[24] != '-' | this._mCode[29] != '-')
                        {
                            result = false;
                        }
                        else
                        {
                            if (this._mCode[1] != 'P' | this._mCode[7] != 'E' | this._mCode[11] != 'R' | this._mCode[15] != 'F' | this._mCode[23] != 'E' | this._mCode[27] != 'C' | this._mCode[33] != 'T')
                            {
                                result = false;
                            }
                            else
                            {
                                string a = this._mCode.Substring(16, 1);
                                if (a != "F" & a != "S" & a != "P" & a != "E" & a != "T" & a != "X" & a != "R" & a != "B")
                                {
                                    result = false;
                                }
                                else
                                {
                                    if (!char.IsNumber(this._mCode[2]) | !char.IsNumber(this._mCode[5]))
                                    {
                                        result = false;
                                    }
                                    else
                                    {
                                        if (!char.IsNumber(this._mCode[12]) | !char.IsNumber(this._mCode[13]))
                                        {
                                            result = false;
                                        }
                                        else
                                        {
                                            if (char.IsDigit(this._mCode[28]))
                                            {
                                                result = false;
                                            }
                                            else
                                            {
                                                if (!char.IsNumber(this._mCode[30]) | !char.IsNumber(this._mCode[31]))
                                                {
                                                    result = false;
                                                }
                                                else
                                                {
                                                    if (Convert.ToInt32(this._mCode[18]) != Convert.ToInt32(this._mCode.Substring(30, 2)))
                                                    {
                                                        result = false;
                                                    }
                                                    else
                                                    {
                                                        if (Convert.ToInt32(this._mCode[28]) != Convert.ToInt32(this._mCode.Substring(12, 2)))
                                                        {
                                                            result = false;
                                                        }
                                                        else
                                                        {
                                                            if (!char.IsNumber(this._mCode[10]))
                                                            {
                                                                result = false;
                                                            }
                                                            else
                                                            {
                                                                if (!Information.IsNumeric(this._mCode.Substring(20, 3)))
                                                                {
                                                                    result = false;
                                                                }
                                                                else
                                                                {
                                                                    if (!Information.IsNumeric(this._mCode.Substring(25, 2)))
                                                                    {
                                                                        result = false;
                                                                    }
                                                                    else
                                                                    {
                                                                        if (char.IsNumber(this._mCode[8]))
                                                                        {
                                                                            result = false;
                                                                        }
                                                                        else
                                                                        {
                                                                            int num = Convert.ToInt32(this._mCode.Substring(25, 2));
                                                                            if (Convert.ToChar(num % 20 + 65) != this._mCode[8])
                                                                            {
                                                                                result = false;
                                                                            }
                                                                            else
                                                                            {
                                                                                if (char.IsNumber(this._mCode[3]))
                                                                                {
                                                                                    result = false;
                                                                                }
                                                                                else
                                                                                {
                                                                                    if (char.IsNumber(this._mCode[32]))
                                                                                    {
                                                                                        result = false;
                                                                                    }
                                                                                    else
                                                                                    {
                                                                                        num = Convert.ToInt32(this._mCode[3]) % 15;
                                                                                        result = (this._mCode[32] == Convert.ToChar(num + 65));
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return result;
        }
        private bool MakeTrial(int ngay)
        {
            this._mLimit = DateTime.Now.AddDays((double)ngay);
            if (Internet.IsConnectedToInternet())
            {
                License license = new License();
                if (license.Get(GridCalendarControlLib.SignSoft() + "-0000-0000-0000-0000-0000-0000", CODE._mComputerCode) == "OK")
                {
                    long num = DateAndTime.DateDiff(DateInterval.Day, license.DateActive, DateTime.Now, FirstDayOfWeek.Monday, FirstWeekOfYear.System);
                    if (num > 30L)
                    {
                        CODE._mMessage = "Phần Mềm đã hết hạn sử dụng.\n\nThông Tin Liên Hệ\n\nCông Ty Phần Mềm Hoàn Hảo \n\nĐịa chỉ: 41 Đường Cách Mạng Tháng 8, Phường 1, Thị xã Tân An, Long An \nĐiện thoại: (072) 6251520 \nFax: (072) 6251519\nEmail: sales@perfect.com.vn\nWebsite: www.perfect.com.vn";
                        this.MakeFree();
                    }
                }
                else
                {
                    if (license.Insert(APP.HELPER.Utils.AssemblyHelper.AssemblyTitle, GridCalendarControlLib.SignSoft() + "-0000-0000-0000-0000-0000-0000", CODE._mComputerCode, true, APP.HELPER.Utils.AssemblyHelper.AssemblyVersion) == "OK")
                    {
                        this.InvokeLicenseChecked();
                    }
                }
            }
            return this.TrialWriteFile("Temp.key") && !this.TrialReadFile("Temp.key", CODE._mComputerCode);
        }
        private bool MakeFree(int free, int typesoft)
        {
            this._mDay = free;
            CODE._mDayEnd = this._mDay;
            this._mLicense = 1;
            CODE._mTypeActive = this._mLicense;
            this._mLimit = DateTime.Now.AddDays((double)free);
            this._mType = typesoft;
            CODE._mTypeSoft = typesoft;
            return this.WriteFile("Perfect.Lic") && !this.ReadFile("Perfect.Lic", CODE._mComputerCode);
        }
        public bool MakeFree()
        {
            return this.MakeFree(-1, 1);
        }
    }
}